package com.wcards.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wcards.config.R;
import com.wcards.entity.CommentInfo;
import com.wcards.mapper.CommentInfoMapper;
import com.wcards.service.CommentInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author Administrator
 */ /*
评论表
 */
@Service
public class CommentInfoServiceImpl extends ServiceImpl<CommentInfoMapper, CommentInfo> implements CommentInfoService {

    @Autowired
    private CommentInfoService commentInfoService;

    //发布评论
    @Override
    public R<String> postComment(Integer animalNumbers, Integer userId, String commentContent)
    {
        //三个参数：动物编号，用户ID，评论内容
        CommentInfo commentInfo = new CommentInfo();
        commentInfo.setAnimalNumbers(animalNumbers);
        commentInfo.setUserId(userId);
        commentInfo.setCommentContent(commentContent);//三个参数赋值
        commentInfo.setCommentTime(LocalDateTime.now());
        if(commentInfoService.save(commentInfo))
        {
            return R.success("评论发布成功");
        }
        return R.error("评论发布失败");
    }

    //根据动物编号查看评论
    @Override
    public R<Page<CommentInfo>> getCommentByAnimalNumbers(Integer animalNumbers, Integer pageSize, Integer pageNumbers)
    {
        LambdaQueryWrapper<CommentInfo> wrapper = new LambdaQueryWrapper<CommentInfo>()
                .eq(CommentInfo::getAnimalNumbers,animalNumbers);
        Page<CommentInfo> page = new Page<>(pageSize,pageNumbers);
        return R.success(commentInfoService.page(page,wrapper));
    }
    //根据用户Id查看评论
    @Override
    public R<Page<CommentInfo>> getCommentByUserId(Integer userId,Integer pageSize,Integer pageNumbers)
    {
        LambdaQueryWrapper<CommentInfo> wrapper = new LambdaQueryWrapper<CommentInfo>()
                .eq(CommentInfo::getUserId,userId);
        Page<CommentInfo> page = new Page<>(pageSize,pageNumbers);
        return R.success(commentInfoService.page(page,wrapper));
    }
    //修改评论
    @Override
    public R<String> updateComment(Integer commentNumbers,String commentContent)
    {
        //查看评论编号是否存在
        CommentInfo commentInfo = commentInfoService.getById(commentNumbers);
        if(commentInfo!=null)
        {
            commentInfo.setCommentContent(commentContent);
            commentInfo.setCommentTime(LocalDateTime.now());
            if(commentInfoService.updateById(commentInfo))
            {
                return R.success("修改成功");
            }
            return R.error("修改失败");
        }
        return R.error("修改失败，评论不存在");
    }

    @Override
    public R<String> deleteComment(Integer commentNumbers)
    {
        if(commentInfoService.removeById(commentNumbers))
        {
            return R.success("删除成功");
        }
        return  R.error("删除失败");
    }
}
